package com.ruoyi.system.service.impl;

import com.ruoyi.common.enums.LevelEnum;
import com.ruoyi.system.domain.MonitorConfig;
import com.ruoyi.system.domain.vo.WarnDigestDTO;
import com.ruoyi.system.domain.vo.WarnDigestDetailVo;
import com.ruoyi.system.monitor.MonitorTypeEnum;
import com.ruoyi.system.service.IMonitorDataService;
import com.ruoyi.system.service.MonitorConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author suhuazhao
 * @date 2023-05-28  17:17
 * @description
 */
@Slf4j
@Service
public class IMonitorDataServiceImpl implements IMonitorDataService {

    @Autowired
    MonitorConfigService monitorConfigService;

    @Override
    public List<WarnDigestDTO> getWarnDigestList() {
        List<WarnDigestDTO> result = new ArrayList<>();
        List<MonitorConfig> monitorConfigs = monitorConfigService.currentList();
        CompletableFuture<?>[] futures = monitorConfigs.stream()
                .map(monitorConfig -> CompletableFuture.runAsync(() -> {
                    MonitorTypeEnum monitor = MonitorTypeEnum.getByName(monitorConfig.getType());
                    result.add(monitor.getService().count(monitorConfig));
                }))
                .toArray(CompletableFuture[]::new);
        try {
            CompletableFuture.allOf(futures).get();
        } catch (InterruptedException | ExecutionException e) {
            log.error("【抓取监控统计】异步任务执行失败！", e);
        }

        result.sort(Comparator.comparing(WarnDigestDTO::getSort));
        return result;
    }

    @Override
    public List<WarnDigestDetailVo> getWarnDetailList(Long configId, LevelEnum type) {
        MonitorConfig monitorConfig = monitorConfigService.getById(configId);
        MonitorTypeEnum monitor = MonitorTypeEnum.getByName(monitorConfig.getType());
        return monitor.getService().detail(monitorConfig, type);
    }


    public static void main(String[] args) {
        String sql = String.format("SELECT\n" +
                "  instance,\n" +
                "  time,\n" +
                "  name,\n" +
                "  value,\n" +
                "  lvl AS \"level\",\n" +
                "  log\n" +
                "FROM (\n" +
                "  SELECT\n" +
                "    b.INSTANCE_NAME AS instance,\n" +
                "    a.TIME AS time,\n" +
                "    a.NAME AS name,\n" +
                "    a.VALUE AS VALUE,\n" +
                "    a.lvl,\n" +
                "    a.DESCRIPTION AS log\n" +
                "  FROM ora_alarm_history a\n" +
                "  LEFT JOIN apm_db_info b ON a.DB_ID = b.DB_ID\n" +
                "  WHERE TIME >= SYSDATE - INTERVAL %s\n" +
                "  UNION ALL\n" +
                "  SELECT \n" +
                "    b.INSTANCE_NAME,\n" +
                "    a.TIME,\n" +
                "    NULL AS NAME,\n" +
                "    NULL AS VALUE,\n" +
                "    a.lvl,\n" +
                "    a.VALUE\n" +
                "  FROM ORA_ALERTLOG_HISTORY a\n" +
                "  LEFT JOIN apm_db_info b ON a.DB_ID = b.DB_ID\n" +
                "  WHERE TIME >= SYSDATE - INTERVAL %s\n" +
                ") T\n" +
                "ORDER BY TIME DESC", "'30' DAY", "'30' DAY");
        System.out.println(sql);
//        SimpleDriverDataSource driverDataSource = new SimpleDriverDataSource();
//        driverDataSource.setDriverClass(oracle.jdbc.driver.OracleDriver.class);
//        String dbUrl = "jdbc:oracle:thin:@//%s:%s/%s";
//        dbUrl = String.format(dbUrl, "192.0.2.11", "1521", "orclpdb1");
//        driverDataSource.setUrl(dbUrl);
//        driverDataSource.setUsername("maxgauge");
//        driverDataSource.setPassword("maxgauge");
//        JdbcTemplate targetTemplate = new JdbcTemplate(driverDataSource);
//
////        String sql = "SELECT e.*, ROWNUM rn FROM (SELECT * FROM ora_alarm_history) e WHERE ROWNUM<=10";
////        String sql = "select count(1) from ora_alarm_history";
////        List<Map<String, Object>> query = targetTemplate.queryForList(sql);
////        System.out.println(query);
//        String interval = "'30' DAY";
//        String sql = String.format("SELECT\n" +
//                " LVL AS severity,\n" +
//                " COUNT(1) AS count,\n" +
//                " MAX(TIME) AS lastestTime\n" +
//                "FROM ora_alarm_history \n" +
//                "WHERE TIME >= SYSDATE - INTERVAL %s \n" +
//                "GROUP BY LVL\n" +
//                "UNION ALL\n" +
//                "SELECT \n" +
//                " LVL AS severity,\n" +
//                " COUNT(1) AS count,\n" +
//                " MAX(TIME) AS lastestTime\n" +
//                "FROM ORA_ALERTLOG_HISTORY\n" +
//                "WHERE TIME >= SYSDATE - INTERVAL %s\n" +
//                "GROUP BY LVL\n", interval, interval);
//        List<MonitorDatabaseCountVo> query = targetTemplate.query(sql, new BeanPropertyRowMapper<>(MonitorDatabaseCountVo.class));
//        WarnDigestDTO dto = mergeMFO(query, new MonitorConfig());
//        System.out.println(dto);
    }


}
